package prepare.huawei;

import java.util.*;

/**
 * description: <a href="https://blog.csdn.net/hihell/article/details/129132563">计算堆栈中剩余的数字</a>
 *
 * @author <a href="zhaoyingling12@163.com">simon zhao</a>
 * @since 2023/3/25
 */
public class EvaluateNumberInStack {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String[] ss = input.nextLine().split(" ");
        int[] numbers = new int[ss.length];
        int i = 0;
        while (i < ss.length) {
            numbers[i] = Integer.parseInt(ss[i]);
            i++;
        }
        Deque<Integer> stack = new LinkedList<>();
        for (int number : numbers) {
            int removeElements = getRemoveElementsNum(stack, number);
            if (removeElements == -1) {
                stack.addLast(number);
                continue;
            }
            for (int k = 1; k <= removeElements; k++) {
                stack.removeLast();
            }
            stack.addLast(2 * number);
        }
        StringBuilder builder = new StringBuilder();
        List<Integer> res = new ArrayList<>(stack);
        for (int m = res.size() - 1; m >= 0; m--) {
            if (m != res.size() - 1) {
                builder.append(" ");
            }
            builder.append(res.get(m));
        }
        System.out.println(builder);
    }

    private static int getRemoveElementsNum(Deque<Integer> stack, int curNum) {
        if (stack.isEmpty()) {
            return -1;
        }
        List<Integer> list = new ArrayList<>(stack);
        int sum = 0, cnt = 0;
        for (int i = list.size() - 1; i >= 0; i--) {
            sum += list.get(i);
            cnt++;
            if (sum == curNum) {
                return cnt;
            }
            if (sum > curNum) {
                return -1;
            }
        }
        return -1;
    }
}
